package com.ocom.webapi.service.impl;

import com.google.gson.Gson;
import com.ocom.common.constant.MapKey;
import com.ocom.common.dto.AddDeptDto;
import com.ocom.common.dto.DeleteDeptDto;
import com.ocom.common.dto.ModifyDeptDto;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.IdentificationCodeUtil;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.SaffPageOnShowVo;
import com.ocom.common.vo.StaffPageVo;
import com.ocom.redis.util.RedisUtil;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.webapi.feign.HumanClientService;
import com.ocom.webapi.service.HumanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class HumanServiceImpl implements HumanService {


    @Autowired
    private HumanClientService humanClientService;


    @Resource
    private RedisUtil redisUtil;


    /**
     * 新增部门
     * */
    @Override
    public Result DepartPageAddDept(AddDeptDto addDeptDto) {
        Map<String, Object> map = new HashMap<>();
        MapKey mapKey = new MapKey();
        map.put(mapKey.getKey_comId(), SecurityUtils.getYoCiUser().getComId());
        map.put(mapKey.getKey_parentDeptId(),addDeptDto.getParentDeptId());
        map.put(mapKey.getKey_deptName(),addDeptDto.getDeptName());
        map.put(mapKey.getKey_remark(),addDeptDto.getRemark());
        map.put("deptType",addDeptDto.getDeptType());
        return humanClientService.AddDeptInfo(map);
    }

    /**
     * 部门列表（DepartPage/onShow）
     * */
    @Override
    public Result DepartPageOnShow(Long deptId, String deptName) {
        return  humanClientService.GetDeptInfo(SecurityUtils.getYoCiUser().getComId(),deptId,deptName);
    }

    /**
     * 修改部门
     * */
    @Override
    public Result DepartPageModify(ModifyDeptDto modifyDeptDto) {
        Map<String, Object> map = new HashMap<>();
        MapKey mapKey = new MapKey();
        map.put(mapKey.getKey_comId(),SecurityUtils.getYoCiUser().getComId());
        map.put(mapKey.getKey_parentDeptId(),modifyDeptDto.getParentDeptId());
        map.put(mapKey.getKey_deptName(),modifyDeptDto.getDeptName());
        map.put(mapKey.getKey_remark(),modifyDeptDto.getRemark());
        map.put(mapKey.getKey_deptId(),modifyDeptDto.getDeptId());
        map.put("deptType",modifyDeptDto.getDeptType());
        return humanClientService.UpdateDept(map);
    }

    /**
     * 删除部门（DepartPage/delete）
     * */
    @Override
    public Result DepartPageDelete(DeleteDeptDto deleteDeptDto) {
        Map<String, Object> map = new HashMap<>();
        MapKey mapKey = new MapKey();
        map.put(mapKey.getKey_comId(),SecurityUtils.getYoCiUser().getComId());
        map.put(mapKey.getKey_deptId(),deleteDeptDto.getDeptId());
        return humanClientService.DelDept(map);
    }

    /**
     * 人员列表（StaffPage/onShow）
     * */
    @Override
    public Result StaffPageOnShow(SaffPageOnShowVo saffPageOnShowVo) {
        Map<String, Object> map = new HashMap<>();
        MapKey mapKey = new MapKey();
        map.put(mapKey.getKey_comId(),SecurityUtils.getYoCiUser().getComId());
        map.put(mapKey.getKey_deptId(),saffPageOnShowVo.getDeptId());
        map.put(mapKey.getKey_pCode(),saffPageOnShowVo.getPCode());
        map.put(mapKey.getKey_pName(),saffPageOnShowVo.getPName());
        map.put(mapKey.getKey_page(),saffPageOnShowVo.getPage());
        map.put(mapKey.getKey_size(),saffPageOnShowVo.getSize());
        return humanClientService.PersonnelGetPersonInfo(map);
    }

    /**
     * 人员信息（StaffPage/info）
     * */
    @Override
    public Result StaffPageInfo(Long comId, Long pId) {
        return humanClientService.GetPersonInfo(SecurityUtils.getYoCiUser().getComId(),pId);
    }
    /**
     * 新增人员信息
     * */
    @Override
    public Result StaffPageAdd(StaffPageVo staffPageVo) {
        Map<String, Object> map = new HashMap<>();
        MapKey mapKey = new MapKey();
        map.put(mapKey.getKey_comId(),SecurityUtils.getYoCiUser().getComId());
        map.put(mapKey.getKey_deptId(),staffPageVo.getDeptId());
        map.put(mapKey.getKey_deptName(),staffPageVo.getDeptName());
        map.put(mapKey.getKey_pId(),staffPageVo.getPId());
        map.put(mapKey.getKey_pCode(),staffPageVo.getPCode());
        map.put(mapKey.getKey_pPwd(),staffPageVo.getPPwd());
        map.put(mapKey.getKey_pName(),staffPageVo.getPName());
        map.put(mapKey.getKey_pSex(),staffPageVo.getPSex());
        map.put(mapKey.getKey_pType(),staffPageVo.getPType());
        map.put(mapKey.getKey_pPhone(),staffPageVo.getPPhone());
        map.put(mapKey.getKey_pIdentity(),staffPageVo.getPIdentity());
        map.put(mapKey.getKey_pBorn(),staffPageVo.getPBorn());
        map.put(mapKey.getKey_pAddress(),staffPageVo.getPAddress());
        map.put(mapKey.getKey_pPhotoUrl(),staffPageVo.getPPhotoUrl());
        map.put(mapKey.getKey_status(),staffPageVo.getStatus());

        if (!IdentificationCodeUtil.isIdentityCode(staffPageVo.getPIdentity())){
            return ResultUtil.error(ResultEnum.Err_2021);
        }
        Gson gson =new Gson();
        Map<String,String> mapUser= new HashMap<>();
        mapUser.put("name",staffPageVo.getPName());
        mapUser.put("identitycard_No",staffPageVo.getPIdentity());
        redisUtil.leftPush("timerCompany:"+staffPageVo.getComId()+":0",gson.toJson(map),82800l);

        return humanClientService.AddPerson(map);
    }

    /**修改人员信息*/
    @Override
    public Result StaffPageModify(StaffPageVo staffPageVo) {
        Map<String, Object> map = new HashMap<>();
        MapKey mapKey = new MapKey();
        map.put(mapKey.getKey_comId(),SecurityUtils.getYoCiUser().getComId());
        map.put(mapKey.getKey_deptId(),staffPageVo.getDeptId());
        map.put(mapKey.getKey_deptName(),staffPageVo.getDeptName());
        map.put(mapKey.getKey_pId(),staffPageVo.getPId());
        map.put(mapKey.getKey_pCode(),staffPageVo.getPCode());
        map.put(mapKey.getKey_pPwd(),staffPageVo.getPPwd());
        map.put(mapKey.getKey_pName(),staffPageVo.getPName());
        map.put(mapKey.getKey_pSex(),staffPageVo.getPSex());
        map.put(mapKey.getKey_pType(),staffPageVo.getPType());
        map.put(mapKey.getKey_pPhone(),staffPageVo.getPPhone());
        map.put(mapKey.getKey_pIdentity(),staffPageVo.getPIdentity());
        map.put(mapKey.getKey_pBorn(),staffPageVo.getPBorn());
        map.put(mapKey.getKey_pAddress(),staffPageVo.getPAddress());
        map.put(mapKey.getKey_pPhotoUrl(),staffPageVo.getPPhotoUrl());
        map.put(mapKey.getKey_status(),staffPageVo.getStatus());

        if (!IdentificationCodeUtil.isIdentityCode(staffPageVo.getPIdentity())){
            return ResultUtil.error(ResultEnum.Err_2021);
        }

        return humanClientService.UpdatePerson(map);
    }





}
